<?php

class Fpt_Business_Block_section_adapter_mysql extends Fpt_Business_Block_section_adapter_abstract
{

    /**
     *
     * @return Fpt_Business_Block_section_adapter_mysql
     */
    public static function getInstance()
    {
        if (!isset(self::$instance) || !self::$instance instanceof self)
            self::$instance = new self;
        return self::$instance;
    }

 

    public function addSection($sectionParams)
    {
        try
        {
            $rs = false;
           
            $objDB = Fpt_Global::getDBConnect('block');
            // Prepare sql
            $stmt = $objDB->prepare('Call sp_insertSection(:p_name, :p_title, :p_desc, :p_layoutname, @p_sectionname);');
            // Bind params
            $stmt->bindParam('p_name', $sectionParams['SectionName'], PDO::PARAM_STR);
            $stmt->bindParam('p_title', $sectionParams['SectionTitle'], PDO::PARAM_STR);
            $stmt->bindParam('p_desc', $sectionParams['SectionDesc'], PDO::PARAM_STR);
            $stmt->bindParam('p_layoutname', $sectionParams['LayoutName'], PDO::PARAM_STR);
            
            // execute
            $stmt->execute();
            
            // Close cursor
            $stmt->closeCursor();

            $stmt = $objDB->query("SELECT @p_sectionname");
            
            $rs = $stmt->fetchColumn();
      
            if(intval($rs) > 0)
            {
                $rs = true;
            }
            // Release
            unset($stmt);
        }
        catch (Exception $ex)
        {
            //log error
            echo $ex->getMessage();
        }
        catch (Zend_Db_Exception $ex)
        {
            //log err
            echo $ex->getMessage();
        }
        return $rs;
    }

    public function getSection($params)
    {
        try
        {
            $rs = false;
            
            $objDB = Fpt_Global::getDBConnect('block');
            // Prepare sql
            $stmt = $objDB->prepare('Call sp_getSectionByName(:p_name, :p_status);');
            // Bind params
            $stmt->bindParam('p_name', $params['SectionName'], PDO::PARAM_STR);
            $stmt->bindParam('p_status', $params['SectionStatus'], PDO::PARAM_INT);
            
            // execute
            $stmt->execute();
            $rs = $stmt->fetchAll();
            // Close cursor
            $stmt->closeCursor();

            // Release
            unset($stmt);
        }
        catch (Exception $ex)
        {
            //log error
            echo $ex->getMessage();
        }
        catch (Zend_Db_Exception $ex)
        {
            //log err
            echo $ex->getMessage();
        }
        return $rs;
    }
    
    
    public function getListSection($layoutName, $offset = null, $limit = null)
    {
        try
        {
            $rs = false;
            $objDB = Fpt_Global::getDBConnect('block');
            // Prepare sql
            $stmt = $objDB->prepare('Call sp_getListSection( :p_offset, :p_limit,:p_layout, @p_rowcount);');
            // Bind params
            $stmt->bindParam('p_layout', $layoutName);
            $stmt->bindParam('p_offset', $offset);
            $stmt->bindParam('p_limit', $limit);

            // execute
            $stmt->execute();
            if($offset == null && $limit == null)
            {
                $rs = $stmt->fetchAll();
                
                return  $rs;
            }
            $rs['data'] = $stmt->fetchAll();
            
            // Close cursor
            $stmt->closeCursor();

            $stmt = $objDB->query("SELECT @p_rowcount");
            
            $rs['total'] = $stmt->fetchColumn();
            
            // Release
            unset($stmt);
        }
        catch (Exception $ex)
        {
            //log error
            echo $ex->getMessage();
        }
        catch (Zend_Db_Exception $ex)
        {
            //log err
            echo $ex->getMessage();
        }
        return $rs;
    }
}